package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.UserQueryObject;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements IUserService {


    @Autowired
    private UserMapper userMapper;

    //新增用户
    @Override
    public void register(User user) {
        //先查询用户输入的邮箱是否存在
        User user1 = userMapper.selectByEmailAndPasswod(user.getEmail(), null);
        if(user1!=null){//如果用户不为空,证明邮箱已存在
            throw new RuntimeException("邮箱已被注册");
        }
        //设置默认的头像 / 背景图片 / 性别
        user.setGender(User.SECRET);
        user.setHeadImgUrl("/img/user/head.jpg");
        user.setCoverImgUrl("/img/user/bg.jpeg");
        try {
            userMapper.insert(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 登录业务
     * @param email 邮箱
     * @param password  密码
     * @return 当前用户
     */
    @Override
    public User login(String email, String password) {
        User user = userMapper.selectByEmailAndPasswod(email, password);
        if(user==null){
            throw new RuntimeException("邮箱或密码错误");
        }
        UserContext.setCurrentUser(user);
        return user;
    }

    /**
     * 注销登录
     */
    @Override
    public void logout() {
        UserContext.setCurrentUser(null);
    }

    /**
     * 高查
     * @param qo : 关键字/分页
     * @return
     */
    @Override
    public PageInfo<User> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<User> list = userMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public void update(User user) {
        try {
            userMapper.updateByPrimaryKey(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取某个用户信息
     * @param id
     * @return
     */
    @Override
    public User get(Long id) {
        return userMapper.selectByPrimaryKey(id);
    }

    /**
     * 获取用户的关注与粉丝数
     * @param id
     * @return
     */
    @Override
    public Map getRelations(Long id) {
        int follow = userMapper.selectFollowCountById(id);
        int fans = userMapper.selectFansCountById(id);
        Map map=new HashMap();
        map.put("follow",follow);
        map.put("fans",fans);
        return map;
    }

    /**
     * 关注时保存两个用户的关系
     * @param userId : 被关注者的id
     * @param followId : 关注者的id
     */
    @Override
    public void saveRelation(Long userId, Long followId) {
        try {
            userMapper.insertRelation(userId,followId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除关注关系
     * @param userId : 被关注者
     * @param followId : 关注者
     */
    @Override
    public void deleteRelation(Long userId, Long followId) {
        try {
            userMapper.deleteRelation(userId,followId);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询两个用户间的关系  followId是否关注了userId
     * @param userId : 被关注者
     * @param followId : 关注者
     * @return
     */
    @Override
    public int getTwoUserRelation(Long userId, Long followId) {
        return userMapper.selectTwoUserRelation(userId,followId);
    }

    @Override
    public PageInfo getUserNickNameCounts(UserQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Strategy> list=userMapper.selectList(qo);
        return new PageInfo<>(list);
    }
}
